﻿Imports DTO, BUS
Imports Microsoft.Office.Interop.Excel
Public Class frmluuthongtinsach

    Private Sub btnluu_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnluu.Click
        Try
            Dim sv As New SaveFileDialog()
            sv.Filter = "Lưu file Excel  .xls | *.xls "

            If sv.ShowDialog() = DialogResult.OK Then
                MessageBox.Show("Chương trình đang lưu file ... ")
                Dim path As String = sv.FileName
                Dim Excel As New Application
                Excel.SheetsInNewWorkbook() = 1
                Excel.Workbooks.Add()
                Excel.Worksheets.Select()
                Excel.Cells(1, 1).Value = "MaSach"
                Excel.Cells(1, 2).VaLue = "TenSach"
                Excel.Cells(1, 3).VaLue = "MaTheLoai"
                Excel.Cells(1, 4).VaLue = "TacGia"
                Excel.Cells(1, 5).VaLue = "NamXuatBan"
                Excel.Cells(1, 6).VaLue = "MaNhaSanXuat"
                Excel.Cells(1, 7).VaLue = "NgayNhap"
                Excel.Cells(1, 8).VaLue = "TriGia"
                Excel.Cells(1, 9).VaLue = "MaNhanVien"
                Excel.Cells(1, 10).VaLue = "MaTinhTrang"
                Excel.Cells(1, 11).VaLue = "SoLuong"
                Dim buss As New SachBUS()
                For i As Integer = 1 To dtgvdssach.RowCount - 1
                    For j As Integer = 0 To dtgvdssach.ColumnCount - 1
                        Excel.Cells(i + 1, j + 1).Value = dtgvdssach.Item(j, i - 1).Value
                    Next
                Next

                Excel.ActiveCell.Worksheet.SaveAs(path)
                Excel.ActiveWorkbook.Close()
                System.Runtime.InteropServices.Marshal.ReleaseComObject(Excel)
                Excel = Nothing
                MessageBox.Show("Lưu thành công !")
            End If

        Catch ex As Exception
            MessageBox.Show("Lưu không thành công ! " & ex.Message)
        End Try

    End Sub

    Private Sub btnthoat_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnthoat.Click
        Close()
    End Sub

    Private Sub frmluuthongtinsach_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Dim buss As New SachBUS()
        dtgvdssach.DataSource = buss.LayDSSachTable()
    End Sub
End Class